package com.ssy.lingxi.member.merchant.utils.baidu;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Aes工具类
 * @author lqa
 * @version 2.0.0
 * @date 2022/1/11
 */
public class AesUtil {
    private static final String ALGORITHM = "AES";

    private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";

    /**
     * aes 加密
     */
    public static byte[] encrypt(byte[] src, byte[] aesKey) throws Exception {
        Cipher cipher = getCipher(aesKey, Cipher.ENCRYPT_MODE);
        return cipher.doFinal(src);
    }

    /**
     * aes 解密
     */
    public static byte[] decrypt(byte[] src, byte[] aesKey) throws Exception {
        Cipher cipher = getCipher(aesKey, Cipher.DECRYPT_MODE);
        return cipher.doFinal(src);
    }

    private static Cipher getCipher(byte[] aesKey, int mode) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
        cipher.init(mode, secretKeySpec);
        return cipher;
    }
}
